<template>
  <div id="addLeavePage">
    <h2 style="margin-bottom: 16px">
      填写请假表
    </h2>
    <!-- 图片信息表单 -->
    <a-form
      name="leaveForm"
      layout="vertical"
      :model="leaveForm"
      @finish="handleSubmit"
    >
      <a-form-item name="name" label="申请人姓名">
        <a-input v-model:value="leaveForm.name" placeholder="请输入申请人姓名" allow-clear />
      </a-form-item>
      <a-form-item name="section" label="部门">
        <a-input v-model:value="leaveForm.section" placeholder="请输入部门" allow-clear />
      </a-form-item>
      <a-form-item name="category" label="分类">
        <a-auto-complete
          v-model:value="leaveForm.category"
          placeholder="请输入分类"
          :options="categoryOptions"
          allow-clear
        />
      </a-form-item>
      <a-form-item name="leaveReason" label="请假事由">
        <a-textarea
          v-model:value="leaveForm.leaveReason"
          placeholder="请输入请假事由"
          :auto-size="{ minRows: 2, maxRows: 5 }"
          allow-clear
        />
      </a-form-item>

      <a-form-item>
        <a-button type="primary" html-type="submit" style="width: 100%">创建</a-button>
      </a-form-item>
    </a-form>

  </div>

</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import { addLeaveUsingPost, listPictureTagCategoryUsingGet } from '@/api/leaveController.ts'
import { message } from 'ant-design-vue'
import router from '@/router'

const leaveForm=reactive<API.LeaveAddRequest>({});


const handleSubmit = async (values: any) => {
    const res=await addLeaveUsingPost(values);

    if(res.data.code===0&&res.data.data){
      message.success("提交成功")
      await router.push(
        { path: '/' }
      )
    }else {
      message.error("提交失败")
    }
}
//请假类型
const categoryOptions = ref<string[]>([])

const getTagCategoryOptions = async () => {
  const res = await listPictureTagCategoryUsingGet()
  if (res.data.code === 0 && res.data.data) {
    categoryOptions.value = (res.data.data.categoryList ?? []).map((data: string) => {
      return {
        value: data,
        label: data,
      }
    })
  } else {
    message.error('获取分类列表失败，' + res.data.message)
  }
}

onMounted(() => {
  getTagCategoryOptions()
})
</script>

<style scoped>
#addLeavePage {
  max-width: 720px;
  margin: 0 auto;
}
#addLeavePage .edit-bar {
  text-align: center;
  margin: 16px 0;
}

</style>

